//
//  AppDelegate.swift
//  H5Package
//
//  Created by QFX on 2023/9/19.
//

import UIKit
import PKHUD
import CoreTelephony

@main
class AppDelegate: UIResponder, UIApplicationDelegate {
    
    
    var window: UIWindow?
    var mianVC:TRWebViewController?
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        netRequest()
        
        Thread.sleep(forTimeInterval: 4.0)
        
        self.window = UIWindow(frame: UIScreen.main.bounds)
        self.window?.backgroundColor = ThemeColor
        
        let cellState = CTCellularData.init()
        cellState.cellularDataRestrictionDidUpdateNotifier = { (dataRestrictedState) in
            DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
                self.window?.rootViewController = self.mianVC
                self.window?.makeKeyAndVisible()
            }
        }
        
        self.mianVC = TRWebBrower.openURL(url:WEBURL)
        HUD.show(.systemActivity,onView: self.mianVC?.webContainer)
        self.mianVC?.webContainer.webViewLoadErrorBlock = { web,err in
            HUD.show(.error)
            HUD.hide(afterDelay: 2)
        }
        self.mianVC?.webContainer.webViewFinishLoadBlock = { web in
            HUD.hide()
        }
        self.window?.rootViewController = self.mianVC
        self.window?.makeKeyAndVisible()
        return true
    }
    
    func netRequest() {
        DispatchQueue.main.asyncAfter(deadline: .now()) {
            DispatchQueue.main.async {
                var request = URLRequest(url: URL(string: "https://www.baidu.com")!, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 30)
                request.httpMethod = "GET"
                URLSession.shared.dataTask(with: request) { data, response, error in
                    if (error != nil) {
                        print("error--:\(String(describing: error))")
                    }
                    print("data:\(String(describing: data))")
                }.resume()
            }
        }
    }
}

